package com.medishare.activity;

import org.json.JSONException;
import org.json.JSONObject;

import publicutils.HealthKnowlInfo;
import publicutils.MediUrl;

import com.medishare.R;
import com.medishare.http.HttpUtils;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.text.Html;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

public class ActivityHealthKnowl extends Activity{
	private ImageView titleImg;
	private TextView titleText;
	private TextView knowTitle;
	private TextView knowName;
	private TextView knowContent;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,   
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.health_knowl);
		findViews();
		initViews();
		
		new AsyncTask<String, Void, HealthKnowlInfo>(){

			@Override
			protected HealthKnowlInfo doInBackground(String... params) {
				// TODO Auto-generated method stub
				TelephonyManager tm = (TelephonyManager) getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
				String id = tm.getDeviceId();
				String reponse = HttpUtils.getReponseByHttpGet(
						MediUrl.base_url+"/News/read/uuid/" + id, 30000);
				Log.d("TAG", id + reponse);
				try {
					JSONObject object = new JSONObject(reponse);
					JSONObject object2 = object.getJSONObject("data");
					int status = object.getInt("status");
					if (status == 1){
						HealthKnowlInfo info = new HealthKnowlInfo();
						info.title = object2.getString("title");
						info.username = object2.getString("user_name");
						info.content = object2.getString("content");
						
						return info;
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				return null;
			}
			
			protected void onPostExecute(HealthKnowlInfo result) {
				Log.d("TAG", "title: " + result.title);
				if (result != null) {
					knowTitle.setText(result.title);
					knowName.setText(result.username);
					knowContent.setText(Html.fromHtml(result.content));
				}
			};
			
		}.execute();
			
			
	}
	
	private void findViews(){
		titleText = (TextView) findViewById(R.id.title);
		knowTitle = (TextView) findViewById(R.id.knowtitle);
		knowName = (TextView) findViewById(R.id.username);
		knowContent = (TextView) findViewById(R.id.content);
		
	}
	
	private void initViews(){
		//titleImg.setBackgroundResource(R.drawable.health_knowl);
		//titleText.setText(R.string.health_knowl);
	}
	
}
